<template>
  <el-container class="home-container">
    <!-- 导航 -->
    <el-header style="height: 50px;">
      <el-dropdown>
        <!-- 用户图像 -->
        <div class="block">
          <el-avatar :size="50" style="cursor: pointer; width: 40px; height: 40px"></el-avatar>
          <span class="userName"></span>
        </div>
      </el-dropdown>
    </el-header>
    <!-- 主体 -->
    <el-container style="height: 100%;">
      <!-- 侧边栏 -->
      <el-aside width="150px" class="fixed-aside">
        <el-menu
          :default-active="activePath"
          :default-openeds="[]"
          router
          background-color="#D0DDF7"
          text-color="#000"
          active-text-color="#E2E9F9"
          active-background-color="#E2E9F9"
          style="height: 100%;"
        >
        <MenuTree :menuList="menuList" />
        </el-menu>
      </el-aside>

      <!-- 主要内容区域 -->
      <el-main class="main-content" style="padding: 0;">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import MenuTree from '../components/MenuTree.vue';
import instance from "../utils/request";

export default {
  components: {
    MenuTree
  },
  data() {
    return {
      menuList: [], // 菜单数据
      activePath: '' // 当前激活的菜单路径
    };
  },
  created() {
    this.getMenuList(); // 在 created 钩子中获取菜单数据
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    async getMenuList() {
      try {
        const response = await instance.get('/tree');
        this.menuList = response.data;
      } catch (error) {
        console.error('获取菜单失败:', error);
      }
    }
  }
};
</script>

<style>
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.fixed-aside {
  height: 650px; /* 根据需要调整高度 */
  background-color: #D0DDF7; /* 设置侧边栏背景色 */
}

.el-menu {
  height: 100%;
}
.el-header {
  z-index: 100;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 19px;
  padding-left: 0px;
  background-color: #D0DDF7; /* 设置导航栏背景色 */
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  width: 100%;
  height: 700px !important;
}
.toggle-btn .el-icon-s-fold,
.toggle-btn .el-icon-s-unfold {
  font-size: 20px;
  margin-top: 5px;
}
.toggle-btn {
  padding-right: 5px;
  background-color: #cccccc !important;
  font-size: 10px;
  line-height: 24px;
  color: #666666;
  text-align: right;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
